package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolPositionMapper;
import cn.da.shuai.cool.system.model.entity.position.CoolPosition;
import cn.da.shuai.cool.system.model.entity.position.SearchPositionVo;
import cn.da.shuai.cool.system.model.entity.position.UpdatePositionVo;
import cn.da.shuai.cool.system.service.service.ICoolPositionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CoolPositionServiceImpl extends CoolServiceImpl<CoolPositionMapper, CoolPosition> implements ICoolPositionService {

    @Override
    public Page<CoolPosition> pageList(SearchPositionVo searchPositionVo, Page page) {
        LambdaQueryWrapper<CoolPosition> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(searchPositionVo.getPositionName() != null, CoolPosition::getPositionName, searchPositionVo.getPositionName())
                .eq(searchPositionVo.getPositionCode() != null, CoolPosition::getPositionCode, searchPositionVo.getPositionCode())
                .eq(searchPositionVo.getStatus() != null, CoolPosition::getStatus, searchPositionVo.getStatus());
        return page(page, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePosition(UpdatePositionVo updatePositionVo) {
        CoolPosition position = new CoolPosition();
        BeanUtils.copyProperties(updatePositionVo, position);
        save(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePosition(UpdatePositionVo updatePositionVo) {
        CoolPosition position = new CoolPosition();
        BeanUtils.copyProperties(updatePositionVo, position);
        updateById(position);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePosition(Long id) {
        removeById(id);
    }

    @Override
    public List<CoolPosition> listAllPositions() {
        return list();
    }
}